package com.example.lycdays.ui.change;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lycdays.R;
import com.example.lycdays.data.bean.Event;
import com.example.lycdays.data.repositiory.EventDataBase;

import java.util.Calendar;

public class ChangeActivity extends AppCompatActivity {

    // 状态码，检测用户是否输入事件名、事件日期
    private static final int NO_EVENT_NAME = 1001;
    private static final int NO_EVENT_DATE = 1002;
    private static final int OTHER = 1000;

    EditText eventName, eventNote;
    Button buttonSubmit;
    View datePicker;
    TextView textViewDate;

    int mYear;
    int mMonth;
    int mDay;
    String eventDate = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);

        Toolbar mToolbar =findViewById(R.id.toolbarLogin);
        mToolbar.setTitle("修改");
        mToolbar.setTitleTextColor(getColor(R.color.colorWhite));
        setSupportActionBar(mToolbar);

        final Event event = (Event) getIntent().getSerializableExtra("event");
        //Toast.makeText(ChangeActivity.this, event.getId() + "", Toast.LENGTH_LONG).show();


        textViewDate = findViewById(R.id.textViewDate);
        eventName = findViewById(R.id.editTextName);
        eventNote = findViewById(R.id.editTextNote);
        buttonSubmit = findViewById(R.id.buttonSubmit);
        eventName.setText(event.getEventName());
        eventNote.setText(event.getEventNotes() == null ? "" : event.getEventNotes());
        eventDate = event.getEventDate();

        textViewDate.setText(eventDate);

        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
        datePicker = findViewById(R.id.datePicker);
        datePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(ChangeActivity.this, onDateSetListener, mYear, mMonth, mDay).show();
            }
        });

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChoosed() == NO_EVENT_NAME) {
                    Toast.makeText(ChangeActivity.this, "请输入事件名", Toast.LENGTH_LONG).show();
                    return;
                }
                if (isChoosed() == NO_EVENT_DATE) {
                    Toast.makeText(ChangeActivity.this, "请选择日期", Toast.LENGTH_LONG).show();
                    return;
                }

                event.setEventName(eventName.getText().toString().trim());
                event.setEventDate(eventDate);
                event.setEventNotes(eventNote.getText().toString().trim());
                EventDataBase.updateEvent(event);
                Toast.makeText(ChangeActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                finish();
            }
        });
    }


    /**
     * 检查用户是否输入事件名、事件时期
     * @return 状态码
     */
    private int isChoosed() {
        if ("".equals(eventName.getText().toString())) {
            return NO_EVENT_NAME;
        }
        if (eventDate == null) {
            return NO_EVENT_DATE;
        }
        return OTHER;
    }

    // 日期选择器的监听
    private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = mYear + "-" + "0" +
                            (mMonth + 1) + "-" + "0" + mDay;
                } else {
                    days = mYear + "-" + "0" +
                            (mMonth + 1) + "-" + mDay;
                }

            } else {
                if (mDay < 10) {
                    days = mYear + "-" +
                            (mMonth + 1) + "-" + "0" + mDay;
                } else {
                    days = mYear + "-" +
                            (mMonth + 1) + "-" + mDay;
                }

            }

            eventDate = days;
            textViewDate.setText(days);
        }
    };
}
